/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
#ifndef PLSTACK_H
#define PLSTACK_H

#include <tools/PLLinkedList.h>

namespace PLSDK {
    
    /**
     * Manage a stack of objects.<br>
     * LIFO (Last In First Out) or<br>
     * FIFO (First In First Out)<br>
     * LIFO is the default value.
     *
     * @ingroup tools_core
     */
    template<typename T>
    class PLStack : public PLLinkedList {
    public:
        /**
         * Constructor.
         *
         * @param fifo FIFO or LIFO version of the stack.
         */
        PLStack(plbool fifo = false) : PLLinkedList<T>(), m_fifo(fifo) {}
        /**
         * Add a element to the stack.
         *
         * @param value The value to add.
         */
        inline void push(T value);
        /**
         * Get the last or first object of the stack.
         *
         * @return The element from the stack.
         */
        inline T pop();
    private:
        plbool m_fifo;
    };

    PLStack::PLStack(plbool fifo = false) : PLLinkedList<T>(), m_fifo(fifo) {
    }

    void PLStack::push(T value) {
        add(value);
    }

    T PLStack::pop() {
        if(m_fifo) {
            T ret = PLList<T>::m_first->next()->content();
            remove(PLList::m_first->next());
            return ret;
        } else {
            T ret = PLLinkedList<T>::m_last->content();
            remove(PLLinkedList<T>::m_last);
            return ret;
        }
    }

}

#endif
